﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DatabaseCore;
using Eaktest;

namespace EakTest
{
    public partial class ChangePinCode : Form
    {
        IUserDao userDao = new UserDao();
        tbl_User userDto = null;

        private int UserId { get; set; }

        public ChangePinCode()
        {
            InitializeComponent();

            UserId = UserContext.Id;

            BindData();
        }

        private void BindData()
        {
            ClearData();

            userDto = userDao.GetUserById(UserId);
            if (userDto == null)
            {
                MessageDialog.Information("User not found");
                return;
            }

            txtEmail.Text = userDto.Email;
        }

        private void ClearData()
        {
            txtEmail.Text = string.Empty;
            txtOldPinCode.Text = string.Empty;
            txtNewPinCode.Text = string.Empty;
            txtConfirmPinCode.Text = string.Empty;
        }

        #region Event
        private void btnSave_Click(object sender, EventArgs e)
        {
            /* Validate before save */
            string NewPinCode = txtNewPinCode.Text.Trim();
            string ConfirmPinCode = txtConfirmPinCode.Text.Trim();

            if (!string.IsNullOrEmpty(NewPinCode) && !string.IsNullOrEmpty(ConfirmPinCode))
            {
                if (NewPinCode != ConfirmPinCode)
                {
                    MessageDialog.Information("Confirm PinCode is not match.");
                    return;
                }
            }

            if (txtOldPinCode.Text.Trim() != userDto.PinCode)
            {
                MessageDialog.Information("Old PinCode is incorrect.");
                return;
            }

            bool isUpdate = userDao.UpdatePinCode(UserId, NewPinCode);
            if (isUpdate)
            {
                MessageDialog.Information("Save completed.");
                this.Close();
            }
            else
            {
                MessageDialog.Information("Cannot change PinCode.");
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearData();
            this.Close();
        }

        private void txtOldPinCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                btnSave.PerformClick();
            }
        }

        private void txtNewPinCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                btnSave.PerformClick();
            }
        }

        private void txtConfirmPinCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                btnSave.PerformClick();
            }
        }
        #endregion
    }
}
